package cn.jbolt.common.poi.excel;

import java.io.ByteArrayOutputStream;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

public class JBoltExcelUtil {
	/**
	 * 处理单元格合并
	 * 
	 * @param excelWriter
	 * @param merges
	 */
	private static void processExcelMerge(ExcelWriter excelWriter, List<JBoltExcelMerge> merges) {
		if (CollUtil.isNotEmpty(merges)){
			merges.forEach(merge -> {
				excelWriter.merge(merge.getFirstRow(), merge.getLastRow(), merge.getFirstColumn(),
						merge.getLastColumn(), merge.getContent(), merge.isSetHeaderStyle());
			});
		}
	}

	/**
	 * 处理表头
	 * @param excelWriter
	 * @param jBoltExcel
	 */
	private static void processExcelHeaders(ExcelWriter excelWriter, JBoltExcel jBoltExcel) {
		List<JBoltExcelHeader> headers = jBoltExcel.getHeaders();
		//设置别名
		for (JBoltExcelHeader header : headers) {
			excelWriter.addHeaderAlias(header.getColumn(), header.getContent());
		}
		//设置只输出设置别名的数据列
		excelWriter.setOnlyAlias(true);
		// 只有merge不设置覆盖header 并且header有值 我才输出header
		if (jBoltExcel.isMergeAsHeader() == false) {
			excelWriter.writeHeadRow(jBoltExcel.getHeaders());
		}
	}

	/**
	 * 处理导出数据
	 * @param excelWriter
	 * @param jBoltExcel
	 */
	private static void processExcelDatas(ExcelWriter excelWriter, JBoltExcel jBoltExcel) {
		if (jBoltExcel.hasDatas()) {
			List<JBoltExcelData> datas=jBoltExcel.getDatas();
			JBoltExcelDataChangeHandler handler=jBoltExcel.getDataChangeHandler();
			if(handler!=null) {
				datas.forEach(data -> handler.change(data));
			}
			excelWriter.write(datas,false);
		}
	}
	/**
	 * 处理列宽
	 * @param excelWriter
	 * @param jBoltExcel
	 */
	public static void processColumnWidth(ExcelWriter excelWriter, JBoltExcel jBoltExcel) {
		if(jBoltExcel.hasHeaders()) {
			List<JBoltExcelHeader> headers=jBoltExcel.getHeaders();
			int size=headers.size(),width;
			for(int i=0;i<size;i++) {
				width=headers.get(i).getWidth();
				if(width>0) {
					excelWriter.setColumnWidth(i, width);
				}else {
					excelWriter.autoSizeColumn(i, true);
				}
			}

		}
	}
	/**
	 * 处理样式
	 * @param excelWriter
	 * @param jBoltExcel
	 */
	private static void processExcelStyle(ExcelWriter excelWriter, JBoltExcel jBoltExcel) {
		processColumnWidth(excelWriter, jBoltExcel);
	}
	/**
	 * 处理导出数据
	 * 
	 * @param excelWriter
	 * @param jBoltExcel
	 *//*
		 * private void processExcelDatas(ExcelWriter excelWriter,JBoltExcel jBoltExcel)
		 * { List<Map<String, Object>> datas=jBoltExcel.getDatas(); if(isOk(datas)) {
		 * List<JBoltExcelHeader> headers=jBoltExcel.getHeaders(); int
		 * row=excelWriter.getCurrentRow(), maxRow=datas.size()+row,
		 * maxCol=jBoltExcel.getHeaders().size(), col=0; Map<String, Object> data;
		 * JBoltExcelHeader header; for(;row<maxRow;row++) { data=datas.get(row);
		 * for(;col<maxCol;col++) { header=headers.get(col);
		 * excelWriter.writeCellValue(row,col, data.get(header.getColumn())); } } } }
		 */

	/**
	 * 把JBoltExcel数据转为byte[]
	 * 
	 * @param jBoltExcel
	 * @return
	 */
	public static byte[] getExcelBytes(JBoltExcel jBoltExcel) {
		ExcelWriter excelWriter = null;
		if(jBoltExcel.isBig()) {
			excelWriter=ExcelUtil.getBigWriter();
		}else {
			excelWriter=ExcelUtil.getWriter(jBoltExcel.isXlsx());
		}
		// 处理单元格合并
		processExcelMerge(excelWriter, jBoltExcel.getMerges());
		// 处理表头生成 如果需要header直接使用merge合并数据作为标题的话 就不处理headers
		processExcelHeaders(excelWriter, jBoltExcel);
		// 处理数据
		processExcelDatas(excelWriter, jBoltExcel);
		//样式处理
		processExcelStyle(excelWriter,jBoltExcel);
		//转为byte[]
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		excelWriter.flush(os, true);
		excelWriter.close();
		return os.toByteArray();
	}
}
